package com.mansoor.app.islamicnames.services;

import com.mansoor.app.islamicnames.beans.Name;
import com.mansoor.app.islamicnames.beans.NameResponse;
import com.mansoor.app.islamicnames.dao.NamesDao;
import org.apache.commons.lang.StringUtils;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import java.net.URLDecoder;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: TKMAQ61
 * Date: Oct 15, 2009
 * Time: 2:23:34 PM
 */
@Path("/nameservice")
public class NameServiceImpl implements NameService
{
    private final NamesDao dao;


    public NameServiceImpl(NamesDao dao)
    {
        this.dao = dao;
    }

    @GET
    @Path("/names/alphabet/{alpha}/page/{page}")
    @Produces("application/json")
    public NameResponse getNames(@PathParam("alpha") String alphabets, @PathParam("page") Integer page)
    {
        final List<Name> names = dao.getNames(alphabets, page);
        NameResponse response = new NameResponse();
        response.setNames(names);
        response.setPage(String.valueOf(page));
        return response;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @GET
    @Path("/names/gender/{gender}/alphabet/{alpha}/page/{page}")
    @Produces("application/json")
    public NameResponse getNames(@PathParam("alpha") String alphabets, @PathParam("gender") String gender, @PathParam("page") Integer page)
    {
        final List<Name> names = dao.getNames(alphabets, gender, page);
        NameResponse response = new NameResponse();
        response.setNames(names);
        response.setPage(String.valueOf(page));
        return response;   //To change body of implemented methods use File | Settings | File Templates.
    }

    @GET
    @Path("/names/name/{name}")
    @Produces("application/json")
    public Name getName(@PathParam("name") String name)
    {

        Name result =  dao.getName(StringUtils.strip(URLDecoder.decode(name)));

        if(result==null)
        {
            result=new Name();
            result.setName("Name Not Found");
            result.setGender("N/A");
            result.setMeaning("N/A");
        }
        return result;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @GET
    @Path("/count/alphabet/{alpha}")
    @Produces("application/json")
    public Integer count(@PathParam("alpha") String alphabets)
    {
        return dao.count(alphabets);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @GET
    @Path("/count/gender/{gender}/alphabet/{alpha}")
    @Produces("application/json")
    public Integer count(@PathParam("alpha") String alphabets, @PathParam("gender") String gender)
    {
        return dao.count(alphabets, gender);  //To change body of implemented methods use File | Settings | File Templates.
    }
}
